package tt.dz.egobus.servicemanager.controller.cartype;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tt.dz.egobus.servicemanager.base.controller.EntityController;
import tt.dz.egobus.servicemanager.base.service.ExtendBaseService;
import tt.dz.egobus.servicemanager.core.exception.BusinessException;
import tt.dz.egobus.servicemanager.entities.BusType;
import tt.dz.egobus.servicemanager.service.BusService;
import tt.dz.egobus.servicemanager.service.BusTypeService;
import tt.dz.egobus.servicemanager.vo.BusTypeViewVo;

import java.util.HashMap;
import java.util.Map;

import static tt.dz.egobus.servicemanager.bean.ServiceManagerCodeStatus.BUS_TYPE_EXIST_BUS;

/**
 * @author Anson
 * @date 17-3-14
 * @name
 * @since 1.0.1
 */
@RestController
@RequestMapping("/busType")
public class BusTypeController extends EntityController<BusType,BusTypeViewVo, String> {

    @Autowired
    @Qualifier("busTypeServiceImpl")
    private BusTypeService service;

    @Autowired
    @Qualifier("busServiceImpl")
    private BusService busService;

    @Override
    protected ExtendBaseService<BusType, String> getSelfService() {
        return this.service;
    }

    @Override
    protected BusTypeViewVo newViewVo() {
        return new BusTypeViewVo();
    }


    /**
     * 删除车型的时候，判断当前是否存在当前车型的车</br>
     * 若存在,则不能删除
     *
     * @param id
     * @throws BusinessException
     */
    @Override
    protected void preDeleteHandler(String id) throws BusinessException {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("eq#busTypeId", id);
        int count = this.busService.count(params);
        if (count > 0) {
            throw new BusinessException(BUS_TYPE_EXIST_BUS.getCodeStatus());
        }
    }
}
